<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        :destroy-on-close="true"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <FormItem
                        :label="t('system.rabbitmq.group_name')"
                        v-model="baTable.form.items!.group_name"
                        type="string"
                        prop="group_name"
                        :placeholder="t('Please input field', { field: t('system.rabbitmq.group_name') })"
                    />
                    <FormItem
                        :label="t('system.rabbitmq.mq_config')"
                        v-model="baTable.form.items!.mq_config"
                        type="string"
                        prop="mq_config"
                        :placeholder="t('Please input field', { field: t('system.rabbitmq.mq_config') })"
                    />
                    <FormItem
                        :label="t('system.rabbitmq.queue_name')"
                        v-model="baTable.form.items!.queue_name"
                        type="string"
                        prop="queue_name"
                        :placeholder="t('Please input field', { field: t('system.rabbitmq.queue_name') })"
                    />
                    <FormItem
                        :label="t('system.rabbitmq.exchange_name')"
                        v-model="baTable.form.items!.exchange_name"
                        type="string"
                        prop="exchange_name"
                        :placeholder="t('Please input field', { field: t('system.rabbitmq.exchange_name') })"
                    />
                    <FormItem
                        :label="t('system.rabbitmq.bind_key')"
                        v-model="baTable.form.items!.bind_key"
                        type="string"
                        prop="bind_key"
                        :placeholder="t('Please input field', { field: t('system.rabbitmq.bind_key') })"
                    />
                    <FormItem
                        :label="t('system.rabbitmq.queue_function')"
                        v-model="baTable.form.items!.queue_function"
                        type="string"
                        prop="queue_function"
                        :placeholder="t('Please input field', { field: t('system.rabbitmq.queue_function') })"
                    />
                    <FormItem
                        :label="t('system.rabbitmq.max_process_num')"
                        v-model="baTable.form.items!.max_process_num"
                        type="string"
                        prop="max_process_num"
                        :placeholder="t('Please input field', { field: t('system.rabbitmq.max_process_num') })"
                    />
                    <FormItem
                        :label="t('system.rabbitmq.max_do_ready_num')"
                        v-model="baTable.form.items!.max_do_ready_num"
                        type="string"
                        prop="max_do_ready_num"
                        :placeholder="t('Please input field', { field: t('system.rabbitmq.max_do_ready_num') })"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import { regularPassword, buildValidatorData } from '/@/utils/validate'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { useAdminInfo } from '/@/stores/adminInfo'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const adminInfo = useAdminInfo()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass
const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    group_name: [buildValidatorData({ name: 'required', title: t('system.rabbitmq.group_name') }), buildValidatorData({ name: 'group_name' })],
    mq_config: [buildValidatorData({ name: 'required', title: t('system.rabbitmq.mq_config') })],
    queue_name: [buildValidatorData({ name: 'required', title: t('system.rabbitmq.queue_name') })],
    exchange_name: [buildValidatorData({ name: 'required', title: t('system.rabbitmq.exchange_name') })],
    bind_key: [buildValidatorData({ name: 'required', title: t('system.rabbitmq.bind_key') })],
    queue_function: [buildValidatorData({ name: 'required', title: t('system.rabbitmq.queue_function') })],
    max_process_num: [buildValidatorData({ name: 'required', title: t('system.rabbitmq.max_process_num') })],
    max_do_ready_num: [buildValidatorData({ name: 'required', title: t('system.rabbitmq.max_do_ready_num') })],
})

// watch(
//     () => baTable.form.operate,
//     (newVal) => {
//         rules.mobile![0].required = newVal == 'Add'
//     }
// )
</script>

<style scoped lang="scss">
.avatar-uploader {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: var(--el-border-radius-small);
    box-shadow: var(--el-box-shadow-light);
    border: 1px dashed var(--el-border-color);
    cursor: pointer;
    overflow: hidden;
    width: 110px;
    height: 110px;
}
.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}
.avatar {
    width: 110px;
    height: 110px;
    display: block;
}
.image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
</style>
